<?php

/**
 * 默认控制器
 */
class Backend_Controller_Htmltemplates extends Backend_Controller_Abstract
{
	function actionIndex()
    {
    	if(isset($_GET['ope'])){
    		if($_GET['ope'] == 'delete'){
    			$id = $this->_context->get('id', null);
    			if($id){
    				Htmltemplates::model()->delete(array('id' => $id));
    			}
    		}
    	}
    	
    	$htmlTemplatesAdded = Htmltemplates::model()->getAll();
    	$htmlTemplatesAdded = Htmltemplates::model()->setResultKey($htmlTemplatesAdded, 'root_foldername');
    	
    	$this->_view['htmlTemplatesAdded'] = $htmlTemplatesAdded;
    	$this->_view['htmltemplateTypes'] = Types::model()->find('*', array('tablename' => 'htmltemplates'), array('groupcolumnname' => true));
    	
    }
    
	function actionSearchnew()
	{
    	$htmlTemplateModel = new Htmltemplates();
    	
    	$addForm = $this->_context->post('form', null);
    	if($addForm){
    		$time = time();
    		$addForm['created_date'] = date('Y-m-d H:i:s', $time);
    		$addForm['created_int'] = $time;
    		$addForm['view_num'] = 0;
    		$addForm['download_num'] = 0;
    		$htmlTemplateModel->attributes = array_merge($htmlTemplateModel->attributes, $addForm);
    		if($result = $htmlTemplateModel->attributesIdentity(array('name', 'root_foldername'))){
    			$result = $htmlTemplateModel->save(true);
    		}
    		$this->_returnAjax(
    			array(
    				'result' => $result,
    				'errorinfo' => $htmlTemplateModel->getValidateError()
    			)		
    		);
    	}
    	
    	$htmlTemplatesUnAdded = $htmlTemplateModel->getUnaddedTemplates();
    	
    	$this->_view['htmlTemplatesUnAdded'] = $htmlTemplatesUnAdded;	
    	$this->_view['htmlTemplateModel'] = $htmlTemplateModel;
    	$this->_view['htmltemplateTypes'] = Types::model()->find('*', array('tablename' => 'htmltemplates'), array('groupcolumnname' => true));
	}
	
	function actionGetdiskinfo()
	{
		$id = $this->_context->get('id', null);
		if($id){
			$ht = Htmltemplates::model()->find('*', array('id' => $id), array('onlyOne' => true));
			
			$htDisk = null;
			if(!empty($ht['root_foldername'])){
				$htDisk = Htmltemplates::model()->getTemplateOnDiskByRootfolder($ht['root_foldername']);
			}
			$this->_returnAjax(array(
				'infoDisk' => $htDisk,
				'infoSaved' => $ht
			));
		}
		
	}	
	
	function actionEdit()
	{
    	$htmlTemplateModel = new Htmltemplates();
    	
	    $editForm = $this->_context->post('form', null);
    	if($editForm){
    		$htmlTemplateModel = new Htmltemplates();
    		$htmlTemplateModel->attributes = $editForm;
    		if(!$htmlTemplateModel->validate()){
				$this->_returnAjax(array(
    				'result' => false,
    				'errorinfo' => $htmlTemplateModel->getValidateError()
	    		));
			}
	   		$this->_returnAjax(array(
    			'result' => $htmlTemplateModel->save(true),
    			'errorinfo' => $htmlTemplateModel->getValidateError()
    		));
    	}
    	
    	return $this->actionSearchnew();
	}
	
	
	
}

